﻿using System.Diagnostics;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.Config;
using WeDonekRpc.PipeServer.FileUp.Model;

namespace WeDonekRpc.PipeServer.Config
{
    public class PipeConfig
    {
        private static string _DefaultPipeName = null;
        private static readonly IConfigSection _Config;
        private static string _ServerKey;
        static PipeConfig ()
        {
            _Config = LocalConfig.Local.GetSection("pipe:Service");
            _ServerKey = _Config.GetValue("ServerKey", "6xy3#7a%ad").GetMd5();
            string def = _Config.GetValue("PipeName", string.Empty);
            if (def == string.Empty)
            {
                using (Process process = Process.GetCurrentProcess())
                {
                    def = process.ProcessName;
                }
            }
            _DefaultPipeName = def;
            _Config.AddRefreshEvent(_Refresh);
        }

        private static void _Refresh (IConfigSection section, string name)
        {
            IsSyncRead = section.GetValue<bool>("IsSyncRead", false);
            ReplyRetryNum = section.GetValue("ReplyRetryNum", 3);
            UpConfig = section.GetValue("UpConfig", new FileUpConfig());
            BufferSize = section.GetValue<int>("BufferSize", 8192);
            ReceiveBufferSize = section.GetValue<int>("ReceiveBufferSize", 32768);
            SendBufferSize = section.GetValue<int>("SendBufferSize", 32768);
        }
        public static int BufferSize { get; private set; } = 8192;

        /// <summary>
        /// 接收缓冲区大小
        /// </summary>
        public static int ReceiveBufferSize { get; private set; } = 32768;

        /// <summary>
        /// 发送缓冲区大小
        /// </summary>
        public static int SendBufferSize { get; private set; } = 32768;
        /// <summary>
        /// 是否同步读
        /// </summary>
        public static bool IsSyncRead { get; private set; }
        /// <summary>
        /// 默认管道名
        /// </summary>
        public static string DefaultPipeName
        {
            get => _DefaultPipeName;
            set => _DefaultPipeName = value;
        }
        private static Interface.IAllot _DefaultAllot = null;

        /// <summary>
        /// 默认的处理程序
        /// </summary>
        public static Interface.IAllot DefaultAllot
        {
            get => PipeConfig._DefaultAllot;
            set => PipeConfig._DefaultAllot = value;
        }

        private static Interface.IPipeEvent _SocketEvent = null;

        /// <summary>
        /// Pipe事件
        /// </summary>
        public static Interface.IPipeEvent PipeEvent
        {
            get => PipeConfig._SocketEvent;
            set => PipeConfig._SocketEvent = value;
        }
        /// <summary>
        /// 服务器的链接密钥
        /// </summary>
        public static string ServerKey
        {
            get => _ServerKey;
            set
            {
                if (value.IsNotNull())
                {
                    _ServerKey = value.GetMd5();
                }
            }
        }


        /// <summary>
        /// 回复重试数
        /// </summary>
        public static int ReplyRetryNum { get; private set; } = 3;

        /// <summary>
        /// 块大小(5MB)
        /// </summary>
        public static FileUpConfig UpConfig
        {
            get;
            private set;
        }
    }
}
